package pt.ua.helpdesk.client;

import java.util.ArrayList;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * This class implements an entry point and should
 * implement the method onModuleLoad<br>
 * <br>
 * This represents the main login page
 * 
 * @author Amigos do Noddy
 */
public class Index implements EntryPoint {
	
	final private static VerticalPanel vpData = new VerticalPanel();
	final private static HorizontalPanel vpLogin = new HorizontalPanel();
	final private static VerticalPanel vpMain = new VerticalPanel();
	final private static RootPanel rootPanel = RootPanel.get();
	
	/**
	 * onModuleLoad handler
	 */
	public void onModuleLoad() {
		
		rootPanel.setWidth("770");

		rootPanel.add(vpMain, 0, 0);
		vpMain.setSize("100%", "100%");
		vpMain.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		vpMain.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		final VerticalPanel verticalPanel_1 = new VerticalPanel();
		vpMain.add(verticalPanel_1);
		verticalPanel_1.setStyleName("loginPanel");
		verticalPanel_1.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		verticalPanel_1.setWidth("100%");


		
		verticalPanel_1.add(vpData);
		vpData.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);


		vpData.add(vpLogin);
		vpLogin.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		final VerticalPanel horizontalPanel_5 = new VerticalPanel();
		vpLogin.add(horizontalPanel_5);
		horizontalPanel_5.setWidth("100%");
		horizontalPanel_5.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		final VerticalPanel verticalPanel_2 = new VerticalPanel();
		horizontalPanel_5.add(verticalPanel_2);
		verticalPanel_2.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);

		final HorizontalPanel horizontalPanel = new HorizontalPanel();
		verticalPanel_2.add(horizontalPanel);
		horizontalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		horizontalPanel.setSpacing(8);

		final Image image = new Image();
		horizontalPanel.add(image);
		image.setUrl("images/logo.png");

		final HorizontalPanel horizontalPanel_1 = new HorizontalPanel();
		verticalPanel_2.add(horizontalPanel_1);
		horizontalPanel_1.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		horizontalPanel_1.setSpacing(2);

		final Label lblUser = new Label("Utilizador:");
		horizontalPanel_1.add(lblUser);
		lblUser.setStyleName("label");

		final TextBox txtUser = new TextBox();
		horizontalPanel_1.add(txtUser);
		txtUser.setText("ceo");
		txtUser.setStyleName("textbox");
		txtUser.setFocus(true);

		final HorizontalPanel horizontalPanel_2 = new HorizontalPanel();
		verticalPanel_2.add(horizontalPanel_2);
		horizontalPanel_2.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		horizontalPanel_2.setSpacing(2);

		final Label lblPass = new Label("Palavra-Chave:");
		horizontalPanel_2.add(lblPass);
		lblPass.setStyleName("label");

		final PasswordTextBox txtPass = new PasswordTextBox();
		horizontalPanel_2.add(txtPass);
		txtPass.setText("ceo");
		txtPass.setStyleName("textbox");

		final HorizontalPanel horizontalPanel_3 = new HorizontalPanel();
		verticalPanel_2.add(horizontalPanel_3);
		horizontalPanel_3.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		horizontalPanel_3.setSpacing(4);

		final Label lblAlerta = new Label("");
		
		final Button entrarButton = new Button();
		horizontalPanel_3.add(entrarButton);
		entrarButton.addClickListener(new ClickListener() {
			public void onClick(final Widget sender) {
        		if((txtUser.getText() == "") || (txtPass.getText() == ""))
        			lblAlerta.setText("Tem de introduzir os seus Dados!");
        		else
        		{
        			ServiceLogin.Util.getInstance().loginCheck(txtUser.getText(),txtPass.getText(), new MyAsyncCallback(lblAlerta));
        		}
			}
		});
		entrarButton.setStyleName("button");
		entrarButton.setText("Entrar");

		final HorizontalPanel horizontalPanel_4 = new HorizontalPanel();
		verticalPanel_2.add(horizontalPanel_4);
		horizontalPanel_4.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		horizontalPanel_4.setSpacing(2);

		horizontalPanel_4.add(lblAlerta);
		lblAlerta.setStyleName("erro");
		
		final HorizontalPanel vpLinks = new HorizontalPanel();
		horizontalPanel_5.add(vpLinks);
		vpLinks.setSpacing(4);

		final Button btnRegistar = new Button();
		vpLinks.add(btnRegistar);
		btnRegistar.addClickListener(new ClickListener() {
			public void onClick(final Widget sender) {
				vpData.clear();
				vpData.add(new LoginRegistar());
			}
		});
		btnRegistar.setWidth("60");
		btnRegistar.setStyleName("linkButton");
		btnRegistar.setText("Registar");

		final Label label = new Label(" |  ");
		vpLinks.add(label);
		label.setStyleName("label");

		final Button btnEsqueceu = new Button();
		vpLinks.add(btnEsqueceu);
		btnEsqueceu.addClickListener(new ClickListener() {
			public void onClick(final Widget sender) {
				vpData.clear();
				vpData.add(new LoginPass());
			}
		});
		btnEsqueceu.setWidth("240");
		btnEsqueceu.setStyleName("linkButton");
		btnEsqueceu.setText("Esqueceu-se da sua Palavra-Chave?");

		final Label label_1 = new Label(" |  ");
		vpLinks.add(label_1);
		label_1.setStyleName("label");

		final Button esqueceuseDaSuaButton = new Button();
		vpLinks.add(esqueceuseDaSuaButton);
		esqueceuseDaSuaButton.addClickListener(new ClickListener() {
			public void onClick(final Widget sender) {
				vpData.clear();
				vpData.add(new LoginActivar());
			}
		});
		esqueceuseDaSuaButton.setStyleName("linkButton");
		esqueceuseDaSuaButton.setText("Activar Conta");
	}
	
	/**
	 * Reloads this page to the root panel
	 */
	public static void reload()
	{
		rootPanel.clear();
		rootPanel.add(vpMain);
	}
	
	/**
	 * Backs to the initial page in the
	 * vertical panel
	 */
	public static void voltar()
	{
		vpData.clear();
		vpData.add(vpLogin);
		
	}
	
	/**
	 * An asynchronous callback is the primary interface that a caller
	 * must implement to receive a response from a remote procedure call
	 * 
	 * This class is the implementation of AsyncCallback for the login
	 * service
	 * 
	 * @author Amigos do Noddy
	 */
	static class MyAsyncCallback implements AsyncCallback
	{
		Label label;
		
		/**
		 * Class constructor
		 * @param label Label for the feedback message
		 */
		public MyAsyncCallback(Label label)
		{
			this.label= label;
		}
		  
		/**
		 * Success handler. Adds the id, username,
		 * and usertype of the person to the session variables
		 */
		public void onSuccess(Object object)
		{
			ArrayList ret = (ArrayList)object;
			String message;
			
			if(ret == null)
	    	{
	    		message = "Dados Incorrectos!";
	    		DOM.setInnerHTML(label.getElement(), message);
	    	}
	    	else
	    	{
	    		//Session session = Session.getInstance();
	    		//System.out.println("IDPerson_CLIENT: " + session.getSession("IDPerson"));
	    		//System.out.println("Tipo_CLIENT: " + session.getSession("Tipo"));
	    		
	    		// loads the default main panel
	    		RootPanel.get().clear();
	    		Default.setSession("IDPerson", ret.get(0).toString());
	    		Default.setSession("Username", ret.get(1).toString());
	    		Default.setSession("Tipo", ret.get(2));
	    		RootPanel.get().add(new Default());
	    	}
		}
		
		/**
		 * Failure handler
		 */
		public void onFailure(Throwable throwable)
		{
			System.out.println("FALHOU!");			
		}
	}  
}
